package controller;

import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import service.postScriptService;
import dto.AddressDTO;
import dto.FileListDTO;
import dto.ListDTO;
import dto.MemberDTO;
import dto.RoomDTO;
import dto.RoomDTO2;
import dto.postScriptDTO;

@Controller
public class postScriptController {
	private postScriptService service;

	public postScriptController(postScriptService service) {
		this.service = service;
	}
	@RequestMapping(value="postScript_writeform.do")
	public ModelAndView postScript_writeform(HttpServletRequest request,
			HttpServletResponse response)throws Exception{
		ModelAndView mv = null;
		int postScript_roomno = Integer.parseInt(request.getParameter("roomno"));
		int postScript_resno = Integer.parseInt(request.getParameter("resno"));
		System.out.println(postScript_resno);
		String postScript_memberid = request.getParameter("postScript_memberid");
		System.out.println(postScript_memberid+","+postScript_roomno);
		try {
			postScriptDTO pdto = new postScriptDTO(postScript_memberid,postScript_roomno,postScript_resno);
			service.searchPicture(pdto);
			return new ModelAndView("postScript_input.sub", "pdto", pdto);
		}catch(Exception e){
			e.printStackTrace();
			mv = new ModelAndView("error.res", "error_message", e.getMessage());
		}
		return mv;
	}
		
		
	
	
	

	@RequestMapping(value = "postScriptWriteController.do", method = RequestMethod.POST)
	public ModelAndView write(postScriptDTO pdto, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = null;
		String postScript_memberid = request.getParameter("postScript_memberid");
		String postScript_content = request.getParameter("postScript_content");
		int postScript_roomno = Integer.parseInt(request.getParameter("roomno"));
		int postScript_score = Integer.parseInt(request.getParameter("score"));
		int postScript_resno = Integer.parseInt(request.getParameter("resno"));
		pdto = new postScriptDTO(postScript_memberid, postScript_score,postScript_content,postScript_roomno,postScript_resno);

		try {
			System.out.println(pdto);
			service.insertContent(pdto);
			
			return new ModelAndView("main.jsp", "pdto", pdto);

		} catch (Exception e) {
			e.printStackTrace();
			mv = new ModelAndView("error.res", "error_message", e.getMessage());
		}
		return mv;
	}

	@RequestMapping("getAllMemberList.do")
	public ModelAndView getAllMemberList() throws Exception {
		ModelAndView mv = null;
		try {
			ArrayList<postScriptDTO> list = service.getAllMemberList();
			return new ModelAndView("postScript_list.jsp", "list", list);
		} catch (Exception e) {
			e.printStackTrace();
			mv = new ModelAndView("error.res", "error_message", e.getMessage());
		}
		return mv;
	}
	/*@RequestMapping("getAllMemberList2.do")
	public ModelAndView getAllMemberList2(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = null;
		String pageTmp = request.getParameter("page");
		int page = 1;
		try{
			page = Integer.parseInt(pageTmp);
		}catch(Exception e){}
		try{
			
		ListDTO ldto = service.getBoardListByPage(page,postScript_no);		
		System.out.println(page);
		ArrayList<postScriptDTO> list = service.getAllMemberList();
		request.setAttribute("list", list);
		request.setAttribute("ldto", ldto);
		request.setAttribute("page", page);
		HashMap map = new HashMap();
		map.put("list",list);
		map.put("ldto", ldto);
		map.put("page",page);
		return new ModelAndView("postScript_list2.jsp", "map", map);
		}catch (Exception e) {
			e.printStackTrace();
			mv = new ModelAndView("error.res", "error_message", e.getMessage());
		}
		return mv;
		
		
	}*/
	
	@RequestMapping(value="getMemberList.do")
	public ModelAndView getMemberList(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = null;
		
		try {
			String postScript_memberid = request.getParameter("postScript_memberid");
			ArrayList<postScriptDTO> list = service.getMemberList(postScript_memberid);
			return new ModelAndView("postScript_id_list.sub", "list", list);
		
		} catch (Exception e) {
			e.printStackTrace();
			mv = new ModelAndView("error.res", "error_message", e.getMessage());
		}
		return mv;
	}

	@RequestMapping(value = "delete.do")
	public ModelAndView delete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = null;
		postScriptDTO pdto = (postScriptDTO) request.getAttribute("pdto");
		try {
			int no = Integer.parseInt(request.getParameter("no"));
			System.out.println("삭제단계" + no);
			service.delete(no);
			ArrayList<postScriptDTO> list = service.getAllMemberList();
			return new ModelAndView("main.jsp");
		} catch (Exception e) {
			e.printStackTrace();
			mv = new ModelAndView("error.res", "error_message", e.getMessage());
		}
		return mv;
	}

	@RequestMapping(value = "modifyForm.do")
	public ModelAndView modifyForm(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = null;
		/*postScriptDTO pdto = (postScriptDTO) request.getAttribute("pdto");*/
		try {
			
			int no = Integer.parseInt(request.getParameter("no"));			
			postScriptDTO pdto = service.getContentByNoForForm(no);			
			mv = new ModelAndView("postScript_modify_form.jsp", "pdto", pdto);

		} catch (Exception e) {
			e.printStackTrace();
			mv = new ModelAndView("error.res", "error_message", e.getMessage());
		}
		return mv;
	}
	@RequestMapping(value="postScriptModifyController.do")
	public ModelAndView modify(HttpServletRequest request, HttpServletResponse response)throws Exception{
		ModelAndView mv= null;
		postScriptDTO pdto = null;

		try{
			int no = Integer.parseInt(request.getParameter("no"));
			String postScript_memberid = request.getParameter("postScript_memberid");
			String postScript_content = request.getParameter("postScript_content");
			System.out.println("memberid : "+postScript_memberid);
			String writedate = null;
			int postScript_score = Integer.parseInt(request.getParameter("score"));
			pdto = new postScriptDTO(no, postScript_memberid,postScript_score, postScript_content,writedate);
			//pdto.setPicture_filename(service.searchPicture(postScript_memberid));
			System.out.println(pdto);
			service.modifyContent(pdto);
			mv = new ModelAndView("main.jsp","pdto",pdto);	
			
		}catch(Exception e){
			e.printStackTrace();
			mv = new ModelAndView("error.res", "error_message", e.getMessage());
		}
		return mv;
	}
	@RequestMapping(value="getContentByRoom2.do")
	public ModelAndView getContentByRoom2(HttpServletRequest request, HttpServletResponse response)throws Exception{
		ModelAndView mv = null;
		String pageTmp = request.getParameter("page");
		int page = 1;
		try{
			page = Integer.parseInt(pageTmp);
			System.out.println(page);
		}catch(Exception e){}
		try {
			String filename = request.getParameter("postScript_roomno");	
		
			int roomno = Integer.parseInt((service.selectRoomnoByFileName(filename)));
			RoomDTO rdto = service.roombyvalue(roomno);
			
			//방번호로 프라이버시, 프로퍼티, 리펀드, 파일리스트 갖고와야함
			
			ArrayList<FileListDTO> fdto = service.searchFileListByRoomNo(roomno);
			//방번호로 파일 이름을 조회한다.
			String pvdto = service.privacybyvalue(roomno);
			//방번호로 프라이버시 조회
			String ppdto = service.propertybyvalue(roomno);
			//방번호로 프라이버티 조회
			String redto = service.refundbyvalue(roomno);
			//방번호로 Refund조회
			ArrayList<postScriptDTO> list= service.postbyvalue(rdto.getRoomNo());
			AddressDTO adto = service.addressbyvalue(roomno);		
			System.out.println("postScript"+list);
			int postScript_roomno = roomno;
			MemberDTO mdto = service.memberbyvalue(rdto.getMemberId());
			ListDTO ldto = service.getBoardListByPage(page,postScript_roomno);
			System.out.println("컨트롤러에서"+ldto);
			HashMap map = new HashMap();			
			map.put("ldto",ldto);
			map.put("room", rdto);
			map.put("post", list);
			map.put("member", mdto);
			map.put("fdto", fdto);
			map.put("pvdto", pvdto);
			map.put("ppdto",ppdto);
			map.put("redto", redto);
			map.put("address", adto);
			map.put("room_no", roomno);
			return new ModelAndView("total.room","map", map);
		
		} catch (Exception e) {
			e.printStackTrace();
			mv = new ModelAndView("error.res", "error_message", e.getMessage());
		}
		return mv;

	}
		
	
	
	@RequestMapping(value="getContentByRoom.do")
	public ModelAndView getContentByRoom(HttpServletRequest request, HttpServletResponse response)throws Exception{
		ModelAndView mv = null;
		String pageTmp = request.getParameter("page");
		String dong = request.getParameter("dong");
		String search = request.getParameter("search");
		int page = 1;
		try{
			page = Integer.parseInt(pageTmp);
			System.out.println(page);
		}catch(Exception e){}
		try {
			int roomno = Integer.parseInt(request.getParameter("postScript_roomno"));	
			RoomDTO2 rdto2 = new RoomDTO2(dong);
			
			RoomDTO rdto = service.roombyvalue(roomno);
			
			//방번호로 프라이버시, 프로퍼티, 리펀드, 파일리스트 갖고와야함
			
			ArrayList<FileListDTO> fdto = service.searchFileListByRoomNo(roomno);
			//방번호로 파일 이름을 조회한다.
			String pvdto = service.privacybyvalue(roomno);
			//방번호로 프라이버시 조회
			String ppdto = service.propertybyvalue(roomno);
			//방번호로 프라이버티 조회
			String redto = service.refundbyvalue(roomno);
			//방번호로 Refund조회
			ArrayList<postScriptDTO> list= service.postbyvalue(rdto.getRoomNo());
			AddressDTO adto = service.addressbyvalue(roomno);
			adto.setSearch(search);
			System.out.println("postScript"+list);
			int postScript_roomno = roomno;
			MemberDTO mdto = service.memberbyvalue(rdto.getMemberId());
			ListDTO ldto = service.getBoardListByPage(page,postScript_roomno);
			System.out.println("컨트롤러에서"+ldto);
			HashMap map = new HashMap();
			map.put("rdto2", rdto2);
			map.put("ldto",ldto);
			map.put("room", rdto);
			map.put("post", list);
			map.put("member", mdto);
			map.put("fdto", fdto);
			map.put("pvdto", pvdto);
			map.put("ppdto",ppdto);
			map.put("redto", redto);
			map.put("address", adto);
			map.put("room_no", roomno);
			return new ModelAndView("total.room","map", map);
		
		} catch (Exception e) {
			e.printStackTrace();
			mv = new ModelAndView("error.res", "error_message", e.getMessage());
		}
		return mv;

	}
	/*@RequestMapping("page.do")
	public  ModelAndView page(HttpServletRequest request, HttpServletResponse response)throws Exception{
		ModelAndView mv= null;
		String pageTmp = request.getParameter("page");
		int page = 1;
		try{
			page = Integer.parseInt(pageTmp);
			System.out.println(page);
		}catch(Exception e){}
		try{
			ListDTO ldto = service.getBoardListByPage(page);			
			mv = new ModelAndView("postScript_list.jsp","ldto",ldto);	
		}catch(Exception e){
			e.printStackTrace();
			mv = new ModelAndView("/error","error_message",e.getMessage());
		}
		return mv;
	}
	@RequestMapping("page1.do")
	public  ModelAndView page1(HttpServletRequest request, HttpServletResponse response)throws Exception{
		ModelAndView mv= null;
		String pageTmp = request.getParameter("page");
		int page = 1;
		try{
			page = Integer.parseInt(pageTmp);
			System.out.println(page);
		}catch(Exception e){}
		try{
			ListDTO ldto = service.getBoardListByPage(page);
			HashMap map = new HashMap();
			map.put("ldto",ldto);
			mv = new ModelAndView("total.jsp","map",map);	
		}catch(Exception e){
			e.printStackTrace();
			mv = new ModelAndView("/error","error_message",e.getMessage());
		}
		return mv;
	}*/
	
	
}
		
		
		
